package controller.v2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import model.CityVO;
import model.LogBuyCashVO;
import model.MemberLevelItemVO;
import model.MemberVO;
import model.ShareVO;
import service.v2.CityService;
import service.v2.ConfigService;
import service.v2.LogExpService;
import service.v2.MemberItemService;
import service.v2.MemberService;
import service.v2.QRCodeService;
import service.v2.ShareService;
import util.PackageUtil;

@Controller
@RequestMapping("/v2/friend")
public class FriendController {
	@Autowired
	private MemberService memberService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private CityService cityService;
	@Autowired
	private QRCodeService qrcodeService;
	@Autowired
	private MemberItemService memberitemservice;
	@Autowired
	private ShareService shareService;
	@Autowired
	private LogExpService logExpService;
	
	/**
	 * 邀请好友二维码
	 */
	@RequestMapping(value="/invitationFriend",method=RequestMethod.GET)
	public String invitationFriend(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response, MemberVO memberVO,String isJSON) throws Exception{
		MemberVO member = memberService.getLoginMemberById(member_id);
			if(os != null){
				os = os.toLowerCase();
			}
			String host = configService.loadConfig("HOST");
			String version=configService.loadConfig("VERSION");
			if(!"1".equals(isJSON)){
				request.setAttribute("member", member);
				request.setAttribute("os", os);
				//邀请好友
				request.setAttribute("friend_list_url",host+version+"/friend/friendList");
				//request.setAttribute("friend_list_url","http://10.0.0.68:8080/v2/friend/friendList");
				request.setAttribute("host_url",host);
				//二维码图片
				request.setAttribute("app_logo",configService.loadConfig("APP_LOGO"));
				request.setAttribute("app_name",configService.loadConfig("APP_NAME"));
				//获取微信公众平台二维码
				/*if("wechat".equals(os)|| "web".equals(os)){
					String fileName = qrcodeService.getQRCodeUrl(member_id);
					request.setAttribute("Wechat_fileName",fileName);
				}*/
				return "/app/friend/invitationFriend";
			}else{
				Map map = new HashMap();
				map.put("member", member);
				map.put("friend_list_url",host+version+"/friend/friendList");
				map.put("app_logo",configService.loadConfig("APP_LOGO"));
				map.put("app_name",configService.loadConfig("APP_NAME"));
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
		        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
		        out.flush();
		        return null;
			}
		
		
	}
	/**
	 * 好友共邀请几个
	 */	
	@RequestMapping(value="/friendList",method=RequestMethod.GET)
	public String friendList(@RequestHeader (value="PAGE", required=false) String page,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response, MemberVO memberVO,String parent_id,String isJSON) throws Exception{
		if(os != null){ 
			os = os.toLowerCase();
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		
		if(!"1".equals(isJSON)){
			
			if(memberVO.getInvited_by()==null || "".equals(memberVO.getInvited_by())){
				memberVO.setInvited_by(member_id);
				request.setAttribute("frist",1);
			}else{
				MemberVO vo = memberService.getInfoById(memberVO.getInvited_by());
				request.setAttribute("member", vo);
			}
			int total = memberService.getTotal(memberVO);
			memberVO.setPagination(memberVO.getPage_index(), total+"");
			List<MemberVO> memberList = memberService.getChildListByParent(memberVO);
			request.setAttribute("memberList", memberList);
			request.setAttribute("os", os);
			request.setAttribute("token", token);
			request.setAttribute("memberId", member_id);
			request.setAttribute("page", page);
			request.setAttribute("total", total);
			request.setAttribute("membervo", memberVO);
			request.setAttribute("friend_list_url",host+version+"/friend/friendList");
			//request.setAttribute("friend_list_url","http://10.0.0.68:8080/v2/friend/friendList");
			request.setAttribute("host_url",host);
		}else{
			Map map  =  new HashMap();

			if(memberVO.getInvited_by()==null || "".equals(memberVO.getInvited_by())){
				memberVO.setInvited_by(member_id);
				map.put("frist",1);
			}else{
				MemberVO vo = memberService.getInfoById(memberVO.getInvited_by());
				map.put("member", vo);
			}
			
			int total = memberService.getTotal(memberVO);
			memberVO.setPagination(memberVO.getPage_index(), total+"");
			List<MemberVO> memberList = memberService.getChildListByParent(memberVO);
			MemberVO pagination = memberService.packPaginationInfo(memberVO);
			map.put("pagination", pagination);
			map.put("memberList", memberList);
			map.put("os", os);
			map.put("token", token);
			map.put("memberId", member_id);
			map.put("page", page);
			map.put("total", total);
			map.put("membervo", memberVO);
			map.put("friend_list_url",host+version+"/friend/friendList");
			map.put("host_url",host);
			response.setContentType("text/json;charset=utf-8");
			
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        return null;
	    }
		return "/app/friend/friendChildList";
	}
	/**
	 * 好友共邀请几个分页
	 */	
	@RequestMapping(value="/friendListItem",method=RequestMethod.GET)
	public String friendListItem(@RequestHeader (value="PAGE", required=false) String page,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response, MemberVO memberVO,String parent_id,String isJSON) throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		if(!"1".equals(isJSON)){
			if(memberVO.getInvited_by()==null){
				memberVO.setInvited_by(member_id);
				request.setAttribute("token", token);
				request.setAttribute("memberId", member_id);
			}else{
				MemberVO vo = memberService.getInfoById(memberVO.getInvited_by());
				request.setAttribute("member", vo);
				request.setAttribute("token", vo.getToken());
				request.setAttribute("memberId", vo.getId());
			}
			int total = memberService.getTotal(memberVO);
			memberVO.setPagination(memberVO.getPage_index(), total+"");
			List<MemberVO> memberList = memberService.getChildListByParent(memberVO);
			request.setAttribute("memberList", memberList);
			request.setAttribute("os", os);
			request.setAttribute("page", page);
			request.setAttribute("friend_list_url",host+version+"/friend/friendList");
			request.setAttribute("host_url",host);
		}else{
			Map map  =  new HashMap();
			
			if(memberVO.getInvited_by()==null){
				memberVO.setInvited_by(member_id);
				map.put("token", token);
				map.put("memberId", member_id);
			}else{
				MemberVO vo = memberService.getInfoById(memberVO.getInvited_by());
				map.put("member", vo);
				map.put("token", vo.getToken());
				map.put("memberId", vo.getId());
			}
			int total = memberService.getTotal(memberVO);
			memberVO.setPagination(memberVO.getPage_index(), total+"");
			List<MemberVO> memberList = memberService.getChildListByParent(memberVO);
			MemberVO pagination = memberService.packPaginationInfo(memberVO);
			map.put("pagination", pagination);
			map.put("memberList", memberList);
			map.put("os", os);
			map.put("page", page);
			map.put("friend_list_url",host+version+"/friend/friendList");
			map.put("host_url",host);
			
		    response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
		    return null;
		}
		return "/app/friend/friendListItem";
	}
	/**
	 * 邀请微信端
	 */	
	@ResponseBody
	@RequestMapping(value="/weChatFriendCome",method=RequestMethod.GET)
	public Object weChatFriendCome(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response, MemberVO memberVO) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		if(memberVO.getInvite_no() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		int reNum = memberService.checkInviteNo(memberVO.getInvite_no());
		if(reNum==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		memberVO = memberService.getSimpleInfoByInviteNo(memberVO.getInvite_no());
		map.put("member_info", memberVO);
		
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	
	/**
	 * 微信端 >app下载
	 * member_id
	 * token
	 * os
	 */	
	@ResponseBody
	@RequestMapping(value="/weChatAppDownload",method=RequestMethod.GET)
	public Object weChatAppDownload(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response, MemberVO memberVO,String friend_invite_no) throws Exception{
	
		Map<String, Object> map = new HashMap<String, Object>();
		
		//安卓>下载appurl
		map.put("app_download_url_android",configService.loadConfig("APP_DOWNLOAD_URL_ANDROID_PAGE"));
		//ios>下载appurl
		map.put("app_download_url_ios",configService.loadConfig("APP_DOWNLOAD_URL_IOS"));
		//友米乐—成就百万精英买手，服务千万品质家庭
		map.put("wechat_share_content",configService.loadConfig("WECHAT_SHARE_CONTENT"));
		map.put("app_logo",configService.loadConfig("APP_LOGO"));
		
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	/**
	 *ZXing生成二维码，并用输出流到jsp页面
	 *@param invite_no
	 *@throws IOException 
	 */	
	 @RequestMapping(value = "/getQ", method={RequestMethod.POST,RequestMethod.GET})
	 public void getqcode(HttpServletResponse response, String invite_no) throws IOException {
	
		String app_logo= configService.loadConfig("APP_LOGO");
		String invite_reg_url= configService.loadConfig("WECHAT_HOST")+"/my_inviteRegister?ym_share_inviteno="+invite_no;
		String app_name=  java.net.URLEncoder.encode(configService.loadConfig("APP_NAME"),"utf-8"); 
			
		String url = invite_reg_url;
		ServletOutputStream stream = null;
        try {
            int width = 1024;//图片的宽度
            int height = 1024;//高度
            stream = response.getOutputStream();
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, height, width);
            MatrixToImageWriter.writeToStream(m, "png", stream);
            
        } catch (WriterException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                stream.flush();
                stream.close();
            }
        }
	  }
	/**
	 * 赠送折扣体验券
	 */
	@RequestMapping(value="/giveDiscountLevel",method=RequestMethod.GET)
	public String giveDiscountLevel(@RequestHeader(value = "LANG", required = false) String lang,
			@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response, MemberVO memberVO,String isJSON) throws Exception{
		MemberVO member = memberService.getLoginMemberById(member_id);
		/*os="ios";
		member_id="3763";
		token="1f0124140a2952bc30b14c161e194e4e";*/
			if(os != null){
				os = os.toLowerCase();
			}
			String host = configService.loadConfig("HOST");
			String version=configService.loadConfig("VERSION");
			memberVO.setMember_id(member_id);
			int total = memberService.getChildListTotal(memberVO);
			memberVO.setPagination(memberVO.getPage_index(), total+"");
			List<MemberVO> childList = memberService.selectChildList(memberVO);
			//查询能赠送体验券列表
			List<MemberLevelItemVO> levelList=memberitemservice.selectLevleList(lang);
			//我当前的折扣ord
		    String myLevel_ord = memberitemservice.getMyLevleOrd(member_id);
			MemberVO vo = memberService.getLevelByMemberId(member_id);

			ShareVO shareVO =shareService.packageshareInfo(vo.getInvite_no());
			if(!"1".equals(isJSON)){
				List<CityVO> cityList = cityService.getTelCountryList();
				List l =    cityService.packageCountryList(cityList);
				request.setAttribute("childList",childList);
				request.setAttribute("levelList",levelList);
				request.setAttribute("myLevel_ord",myLevel_ord);
				request.setAttribute("token", token);
				request.setAttribute("memberId", member_id);
				request.setAttribute("os", os);
				request.setAttribute("cityList", l);
				request.setAttribute("shareVO", shareVO);
				request.setAttribute("member_level",vo.getLevel());
				request.setAttribute("ticketGiveMore_url",host+version+"/friend/ticketGiveMore");
				//request.setAttribute("ticketGiveMore_url","http://10.0.0.68:8080/v2/friend/ticketGiveMore");
				return "/app/friend/giveDiscountLevel";
			}else{
				Map map  =  new HashMap();
				 map.put("childList",childList);
				 map.put("levelList",levelList);
				 map.put("myLevel_ord",myLevel_ord);
				 map.put("token", token);
				 map.put("memberId", member_id);
				 map.put("os", os);
				 map.put("shareVO", shareVO);
				 map.put("pagination", memberVO); 

				
				response.setContentType("text/json;charset=utf-8");
		        PrintWriter out = response.getWriter();
		        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
		        out.flush();
		        out.close();
			    return null;
			}
		
	}
	/**
	 * 赠送折扣体验劵分页
	 */
	@RequestMapping(value="/giveDiscountLevelItem",method=RequestMethod.GET)
	public String giveDiscountLevelItem(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response, MemberVO memberVO,String isJSON) throws Exception{
		MemberVO member = memberService.getLoginMemberById(member_id);
		/*os="ios";
		member_id="751";
		token="e54f8ccaf351d9b4e47dc41fd7c2e3f7";*/

		if(os != null){
			os = os.toLowerCase();
		}
		memberVO.setMember_id(member_id);
		int total = memberService.getChildListTotal(memberVO);
		memberVO.setPagination(memberVO.getPage_index(), total+"");
		List<MemberVO> childList = memberService.selectChildList(memberVO);
		//查询能赠送体验券列表
		List<MemberLevelItemVO> levelList=memberitemservice.selectLevleList(lang);
		//我当前的折扣ord
		String myLevel_ord = memberitemservice.getMyLevleOrd(member_id);
		if(!"1".equals(isJSON)){
			request.setAttribute("childList",childList);
			request.setAttribute("levelList",levelList);
			request.setAttribute("myLevel_ord",myLevel_ord);
			request.setAttribute("token", token);
			request.setAttribute("memberId", member_id);
			request.setAttribute("os", os);
			return "/app/friend/giveDiscountLevelItem";
		}else{
			Map map  =  new HashMap();
			map.put("childList",childList);
			map.put("levelList",levelList);
			map.put("myLevel_ord",myLevel_ord);
			map.put("token", token);
			map.put("memberId", member_id);
			map.put("os", os);
			map.put("pagination", memberVO); 
			
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
			out.flush();
			out.close();
			return null;
		}
		
	}
	/**
	 * 
	 */	
	
	@RequestMapping(value="/checkChildId",method=RequestMethod.GET)
	public String checkChildId(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request,
			HttpServletResponse response,MemberVO memberVO,String id,String myLevel_ord,String tel,String country,String isJSON)throws IOException{
		/*os="ios";
		member_id="3404";
		token="5600f9daffcb3c6e9f247200600c643f";*/
		if(os != null){
			os = os.toLowerCase();
		}
		MemberVO childVo=new MemberVO();
		String childLevel_ord = "";
		if(country !=null && tel !=null){
			childVo=memberService.getAllInfoByTelAndCountry_id(tel,country);
			if(childVo==null){
				response.setContentType("text/json;charset=utf-8");   
				PrintWriter out = response.getWriter(); 
				out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友不是您邀请的好友或不存在", "", "")));
				out.flush();
				out.close();
				return null;
			}else{
				childLevel_ord = memberitemservice.getMyLevleOrd(childVo.getId());
			}
		}else{
			 childVo=memberService.getAllInfoById(id);
				//下线的折扣ord
				 childLevel_ord = memberitemservice.getMyLevleOrd(id);
		}
		//上下线关系变更时
		if(!member_id.equals(childVo.getInvited_by())){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友不是您邀请的好友或不存在", "", "")));
			out.flush();
			out.close();
			return null;
		}
		
		//下线级别高于赠送人时		
		if(Integer.parseInt(childLevel_ord)>Integer.parseInt(myLevel_ord)){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友不是您邀请的好友或不存在", "", "")));
			out.flush();
			out.close();
			return null;
		}
		if("1".equals(childVo.getExp_state())){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(16004, "该会员处于折扣券体验期，无法重复赠送", "", "")));
			out.flush();
			out.close();
			return null;
		}
		
		//没有可赠送的级别时
		int state = memberitemservice.getStateBySomeField(childVo.getId(),member_id);
		if(state==0){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友无法赠送", "", "")));
			out.flush();
			out.close();
			return null;
		}
		if(!"120".equals(childVo.getIdentity())){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友不是您邀请的好友或不存在", "", "")));
			out.flush();
			out.close();
			return null;
		}
		List<MemberLevelItemVO> levelList=memberitemservice.selectLevleListBySomeField(childVo.getId(),member_id);

		if(!"1".equals(isJSON)){
			request.setAttribute("levelList", levelList);
			request.setAttribute("childId",childVo.getId());	
			 return "/app/friend/ajaxReturnLevelList";
		}else{
			Map map  =  new HashMap();
				
			map.put("levelList", levelList);	
			map.put("childId", childVo.getId());	
			
			response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        out.close();
		    return null;
		}
	}
	/**
	 * 赠送折扣体验券
	 */
	@ResponseBody
	@RequestMapping(value="/updateChildLevel",method=RequestMethod.POST)
	public Object updateChildLevel(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response,String child_member_id,String level) throws Exception{
		Map map  =  new HashMap();
		String child_id[]=child_member_id.split(",");
		//我当前的折扣ord
		String myLevel_ord = memberitemservice.getMyLevleOrd(member_id);
		for(int i=0; i<child_id.length;i++){
			MemberVO childVo=memberService.getAllInfoById(child_id[i]);
			//下线的折扣ord
			String childLevel_ord = memberitemservice.getMyLevleOrd(child_id[i]);
			//曾今赠送过相同的折扣时
			int log_exp_count =logExpService.selectLogExpCountLevel(member_id,child_id[i],level);
			
			if(log_exp_count>0){
				return PackageUtil.packageParams(16002,"该好友不是您邀请的好友或不存在","","");
			}
			//上下线关系变更时
			if(!member_id.equals(childVo.getInvited_by())){
				
				return PackageUtil.packageParams(16002,"该好友不是您邀请的好友或不存在","","");
			}
			//体验期会员时
			if("1".equals(childVo.getExp_state())){
				return PackageUtil.packageParams(16004,"该会员处于折扣券体验期，无法重复赠送","","");
			}
			if(!"120".equals(childVo.getIdentity())){
				response.setContentType("text/json;charset=utf-8");   
				PrintWriter out = response.getWriter(); 
				out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友不是您邀请的好友或不存在", "", "")));
				out.flush();
				out.close();
				return null;
			}
			if(Integer.parseInt(childLevel_ord)>Integer.parseInt(myLevel_ord)){
				return PackageUtil.packageParams(16002,"该好友不是您邀请的好友或不存在","","");
			}
			//没有可赠送的级别时
			int state = memberitemservice.getStateBySomeField(child_id[i],member_id);
			if(state==0){
				response.setContentType("text/json;charset=utf-8");   
				PrintWriter out = response.getWriter(); 
				out.print(JSON.toJSONString(PackageUtil.packageParams(16002, "该好友无法赠送", "", "")));
				out.flush();
				out.close();
				return null;
			}
		}
		for(int i=0; i<child_id.length;i++){
			int state = memberService.updateChildLevel(child_id[i],level);
			if(state ==0){
				return PackageUtil.packageParams(16003,"赠送失败或网络问题未能完成操作，请稍候重试","","");
			}else{
				memberService.insertLogExp(child_id[i],level,member_id);
				memberService.updateExpMsg(child_id[i]);
				MemberVO childVo=memberService.getAllInfoById(child_id[i]);
				map.put("level_name",childVo.getLevel_name());
				map.put("name",childVo.getNick_name());
				map.put("tel_h",childVo.getTel_h());
			}
		}
		return PackageUtil.packageParams(16001,"赠送成功",map,"");
		
	}
	/**
	 * 批量下一步赠送体验券画面
	 */	
	@RequestMapping(value="/ticketGiveMore",method=RequestMethod.GET)
	public String ticketGiveMore(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request,
			HttpServletResponse response,MemberVO memberVO,String isJSON,String level)throws IOException{
		/*os="ios";
		member_id="3242";
		token="bb40c7d4b8c785576cbbadd4f926e802";
		level="123";*/
		if(os != null){
			os = os.toLowerCase();
		}
		List<MemberVO> childList = new ArrayList<MemberVO>();
		memberVO.setMember_id(member_id);
		memberVO.setLevel(level);
		int total = memberService.getChildListTotalByLevel(memberVO);
		memberVO.setPagination(memberVO.getPage_index(), total+"");
		childList = memberService.selectChildListByLevle(memberVO);
		String rate=memberitemservice.getRateByLevel(level);
		if(!"1".equals(isJSON)){
			if(os != null){	
				os = os.toLowerCase();
			}
			request.setAttribute("childList",childList); 
			request.setAttribute("rate",Double.valueOf(rate)*10); 
			request.setAttribute("childList",childList); 
			request.setAttribute("token", token);
			request.setAttribute("os", os);
			request.setAttribute("memberId", member_id);
			request.setAttribute("level", memberVO.getLevel());
			return "/app/friend/ticketGiveMore";
		}else{
			Map map  =  new HashMap();
			 map.put("childList",childList); 
			 map.put("rate",(Double.valueOf(rate)*10)+""); 
			 map.put("token", token);
			 map.put("os", os);
			 map.put("memberId", member_id);
			 map.put("pagination", memberVO); 
			
			response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        out.close();
		    return null;
		}
	}
	/**
	 * 批量下一步赠送体验券画面
	 */	
	@RequestMapping(value="/ticketGiveMoreItem",method=RequestMethod.GET)
	public String ticketGiveMoreItem(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request,
			HttpServletResponse response,MemberVO memberVO,String isJSON,String level)throws IOException{
		/*os="ios";
		member_id="3208";
		token="22221994d60a54a46171224fe3e9da70";*/
		if(os != null){
			os = os.toLowerCase();
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		
		List<MemberVO> childList = new ArrayList<MemberVO>();
		memberVO.setMember_id(member_id);
		memberVO.setLevel(level);
		int total = memberService.getChildListTotalByLevel(memberVO);
		memberVO.setPagination(memberVO.getPage_index(), total+"");
		childList = memberService.selectChildListByLevle(memberVO);
		
		if(!"1".equals(isJSON)){
			if(os != null){
				os = os.toLowerCase();
			}
			request.setAttribute("childList",childList); 
			request.setAttribute("token", token);
			request.setAttribute("os", os);
			request.setAttribute("memberId", member_id);
			
			request.setAttribute("buycashheleppage_url",host+version+"/page/buyCashHelp");
			return "/app/friend/ticketGiveMoreItem";
		}else{
			Map map  =  new HashMap();
			map.put("", ""); 
			map.put("token", token);
			map.put("os", os);
			map.put("memberId", member_id);
			map.put("pagination", memberVO); 
			
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
			out.flush();
			out.close();
			return null;
		}
	}

}
